/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAL;

import Entity.Customer;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author MrKoi
 */
public class CustomerBean {
    Collection listProfile;
    Collection ListCustomer;
    Connect c=new Connect();
    Classshare cs=new Classshare();
    Connection con;
    CallableStatement cl=null;
    public CustomerBean ( ){}
    private void closeCnn(){
        try {
            cl.close();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(AdminBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public boolean RegistryCustomer(String user,String pass,String fullname,String gender,String email,String country,String address, String phone)
    {
        con=c.getConnection();
        try
        {
            String p=cs.MD5(pass);
            cl=con.prepareCall("{call RegistryCustomer_sp(?,?,?,?,?,?,?,?)}");
            cl.setString(1,user);
            cl.setString(2,p);
            cl.setString(3,fullname);
            cl.setString(4,gender);
            cl.setString(5,email);
            cl.setString(6,country);
            cl.setString(7,address);
            cl.setString(8,phone);
            int result=cl.executeUpdate();
            if(result >0)
                return true;
            closeCnn();
        }catch (Exception ex){closeCnn();return false;}
        return false;
    }
    //update pass khi forget password
    public boolean FoegetPass(String pass,String user,String email, String phone)
    {
        con=c.getConnection();
        try
        {
            //String p=cs.MD5(pass);
            cl=con.prepareCall("{call FoegetPass_sp(?,?,?,?)}");
            cl.setString("PassWord",cs.MD5(pass));
            cl.setString("UserName",user);
            cl.setString("Email",email);
            cl.setString("Phone",phone);
            int rs=cl.executeUpdate();
            if(rs>0)
            {
                return true;
            }
            closeCnn();
        }catch (Exception ex){closeCnn();return false;}
        return false;
    }
    public boolean CheckExitCustomer(String u)
    {
        con=c.getConnection();
         try
         {
            cl=con.prepareCall("{call CheckExitCustomer_sp(?)}");
            cl.setString(1,u);
            ResultSet rs=cl.executeQuery();
            while(rs.next())
            {
                 if(rs.getString("UserName").equals(u))
                 {
                       return true;
                 }
                 else
                     return false;
            }
            closeCnn();
         }catch (Exception ex){closeCnn();return false;}
        return false;
    }
    public boolean LoginCustomer(String u,String p)
    {
         con=c.getConnection();
         try
         {
            String pass=cs.MD5(p);
            cl=con.prepareCall("{call LoginCustomer_sp(?,?)}");
            cl.setString(1,u);
            cl.setString(2,pass);
            ResultSet rs=cl.executeQuery();
            while(rs.next())
            {
                 if(rs.getString("UserName").equals(u)&&rs.getString("PassWord").equals(pass))
                 {
                       return true;
                 }
                 else
                     return false;
            }
            closeCnn();
         }catch (Exception ex){closeCnn();return false;}
        return false;
    }
     public Collection SelectCustomers()
    {
         con=c.getConnection();
        ListCustomer=new ArrayList();
        try
        {
            cl=con.prepareCall("{call SelectCustomers_sp}");
            ResultSet rs=cl.executeQuery();
            
            while(rs.next())
            {
                Customer cus=new Customer();
                cus.setUserName(rs.getString("UserName"));
                cus.setGender(rs.getString("Gender"));
                cus.setPassWord(rs.getString("PassWord"));
                cus.setFullName(rs.getString("FullName"));
                cus.setEmail(rs.getString("Email"));
                cus.setCountry(rs.getString("Country"));
                cus.setAddress(rs.getString("Address"));
                cus.setPhone(rs.getString("Phone"));
                ListCustomer.add(cus);
            }
            closeCnn();
        }catch (Exception ex){closeCnn();}
        return ListCustomer;
    }
    public Collection SelectProfile(String user)
    {
         con=c.getConnection();
        listProfile=new ArrayList();
        try
        {
            cl=con.prepareCall("{call SelectProfile_sp(?)}");
            cl.setString(1,user);
            ResultSet rs=cl.executeQuery();            
            while(rs.next())
            {
                Customer cus=new Customer();
                cus.setUserName(rs.getString("UserName"));
                cus.setPassWord(rs.getString("PassWord"));
                cus.setFullName(rs.getString("FullName"));
                cus.setGender(rs.getString("Gender"));
                cus.setEmail(rs.getString("Email"));
                cus.setCountry(rs.getString("Country"));
                cus.setAddress(rs.getString("Address"));
                cus.setPhone(rs.getString("Phone"));
                listProfile.add(cus);
            }
            closeCnn();
        }catch (Exception ex){closeCnn();}
        return listProfile;
    }
    public boolean UpdateCustomerPass(String user,String passold,String passnew)
    {
        con=c.getConnection();
        try
        {
                cl=con.prepareCall("{call UpdateCustomerPass_sp(?,?,?)}") ;
                cl.setString("PassWordNew",cs.MD5(passnew));
                cl.setString("UserName",user);  
                cl.setString("PassWord",cs.MD5(passold));
            int rs=cl.executeUpdate();
            if(rs > 0)
                return true;     
           closeCnn();
        }catch (Exception ex){closeCnn();return false;}
        return false;       
    }
     public boolean UpdateCustomerNoPass(String user,String fullname,String gender,String email,String country,String address, String phone)
    {
        con=c.getConnection();
        try
        {
                cl=con.prepareCall("{call UpdateCustomerNoPass_sp(?,?,?,?,?,?,?)}");
                cl.setString("FullName",fullname);
                cl.setString("Country",country);
                cl.setString("Email",email);
                cl.setString("Country",country);
                cl.setString("Address",address);
                cl.setString("Phone",phone);
                cl.setString("UserName",user);           
            int rs=cl.executeUpdate();
            if(rs > 0)            
                return true;       
          closeCnn();
        }catch (Exception ex){closeCnn();return false;} 
        return false;      
    }
     public boolean DeleteCustomer(String user)
     {
        con=c.getConnection();
        try
        {
            cl=con.prepareCall("{call DeleteCustomer_sp(?)}");
            cl.setString(1,user);
            int rs=cl.executeUpdate();
            if(rs >0)
                return true;
            closeCnn();
        }catch (Exception ex){closeCnn();return false;  }
        return false;  
     }
}
